package soulkun.library.helper.decoration

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import soulkun.library.utils.toPx

/**
 * LinearLayoutManager布局和GridLayoutManager布局的间隔通用实现类
 * 其中填入的数字已自动转成Px，无需事先转化
 * 注意，GridLayoutManager只考虑一行全占满和只占一格的情况，横跨多格导致的换行等情况不考虑
 * GridLayoutManager：竖直布局——第一行 top:space bottom:space 其他 bottom:space
 *                    水平布局——第一列 left:space right:space 其他 right:space
 */
class GridSpaceItemDecorationHelper(
    private val verticalSpaceDp: Int,
    private val horizontalSpaceDp: Int
) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val layoutManager = parent.layoutManager
        if (layoutManager is GridLayoutManager) {
            val adapterPosition = parent.getChildAdapterPosition(view)
            val layoutParams = view.layoutParams as GridLayoutManager.LayoutParams
            val spanCount = layoutManager.spanCount
            //竖直方向
            if (layoutManager.orientation == GridLayoutManager.VERTICAL) {
                when (layoutManager.itemCount) {
                    0 -> {
                    }
                    else -> {
                        //是否在第一行
                        if (layoutManager.spanSizeLookup.getSpanGroupIndex(
                                adapterPosition,
                                spanCount
                            ) == 0
                        ) {
                            outRect.top = verticalSpaceDp.toPx()
                        }
                        outRect.bottom = verticalSpaceDp.toPx()
                        //考虑占满和单一的问题
                        if (layoutParams.spanSize == spanCount) { //占满
                            outRect.left = horizontalSpaceDp.toPx()
                            outRect.right = horizontalSpaceDp.toPx()
                        } else {
                            outRect.left =
                                ((spanCount - layoutParams.spanIndex).toFloat() / spanCount * horizontalSpaceDp.toPx()).toInt()
                            outRect.right =
                                (horizontalSpaceDp.toPx() * (spanCount + 1) / spanCount - outRect.left)
                        }
                    }
                }
            }
            //水平方向
            else {
                when (layoutManager.itemCount) {
                    0 -> {
                    }
                    else -> {
                        //是否在第一行
                        if (layoutManager.spanSizeLookup.getSpanGroupIndex(
                                adapterPosition,
                                spanCount
                            ) == 0
                        ) {
                            outRect.left = horizontalSpaceDp.toPx()
                        }
                        outRect.right = horizontalSpaceDp.toPx()
                        //考虑占满和单一的问题
                        if (layoutParams.spanSize == spanCount) { //占满
                            outRect.top = verticalSpaceDp.toPx()
                            outRect.bottom = verticalSpaceDp.toPx()
                        } else {
                            outRect.top =
                                ((spanCount - layoutParams.spanIndex).toFloat() / spanCount * verticalSpaceDp.toPx()).toInt()
                            outRect.right =
                                (verticalSpaceDp.toPx()
                                    .toFloat() * (spanCount + 1) / spanCount - outRect.top).toInt()
                        }
                    }
                }
            }
        }
    }
}